﻿using Castle.DynamicProxy;
using Microsoft.AspNetCore.Mvc.ActionConstraints;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Framework.LinkTrackingLog.MyTest
{
    /// <summary>
    /// 注册服务
    /// </summary>
    public static class RegistrationService
    {
        public static void Registration(this IServiceCollection services, Action<MethodMonitoringModel> logAction)
        {
            //注册方法监控服务
            services.AddSingleton<IMethodMonitoringService>(provider => new MethodMonitoringService(logAction));
            //给所有自定义方法（非系统方法）注册自定义拦截器（MethodMonitoringInterceptor）
            services.AddSingleton<IInterceptor, MethodMonitoringInterceptor>();
            //给API方法注册方法监控过滤器
            services.AddTransient<IActionFilter, MethodMonitoringActionFilter>();

            services.AddScoped<ProxyGenerator>();
        }

        private static void Registration(this IServiceCollection services)
        {
            //获取程序集中所有方法，并添加方法监控过滤器
            AppDomain.CurrentDomain.GetAssemblies().Where(x=>x.CustomAttributes.Any(y=>y.AttributeType == typeof(ControllerAttribute)))

        }

        private static void AutoInterceptAllServices(this IServiceContainer service)
        {

        }
    }
}
